
export class InMemoryCache<T> {
    ttl: number;
    values: Map<string, T> = new Map();

    constructor(ttl: number = 0) {
        this.ttl = ttl;
    }

    async get(key: string, callback: () => Promise<T>): Promise<T> {
        let value = this.values.get(key);
        if (!!value) {
            return Promise.resolve(value);
        } else {
            value = await callback();
            this.values.set(key, value);
            if (this.ttl > 0) {
                setTimeout(() => {
                    this.values.delete(key);
                }, this.ttl);
            }
            return value;
        }
    }

}